package com.cn.yingu.controller;

import com.cn.yingu.pojo.VersionInfo;
import com.cn.yingu.service.VersionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/VersionInfoAPI")
public class VersionInfoApiController {
    @Autowired
    private VersionInfoService versionInfoService;

    /**
     * 列表查询
     *
     * @param param
     * @param request
     * @return
     */
    @RequestMapping(value = "/List_Ajax", method = RequestMethod.POST)
    public ListPage List_Ajax(@RequestBody HashMap<String, Object> param, HttpServletRequest request) {
        try {
            int pageSize = (null != param.get("pageSize")) ? (Integer) param.get("pageSize") : 0;
//            int pageSize = (null != param.get("pageSize") && 0 != param.get("pageSize").trim().length())
//                    ? Integer.parseInt(param.get("pageSize")) : null;
            int pageIndex = (null != param.get("pageIndex")) ? (Integer) param.get("pageIndex") : 0;
            String VersionName = param.get("VersionName").toString();
            Map<String, Object> searchParams = new HashMap<String, Object>();
            searchParams.put("pageSize", pageSize);
//            searchParams.put("pageIndex", pageIndex);
            int limitCount = (pageIndex - 1) * pageSize;
            searchParams.put("limitCount", limitCount);
            searchParams.put("VersionName", VersionName);

            int total = versionInfoService.ListPageCount(searchParams);//数据总数
            List<VersionInfo> list = versionInfoService.ListPage(searchParams);

//            int totalPage = 0;//页码总数
//            if (total % pageSize == 0) {
//                totalPage = total / pageSize;
//            } else {
//                totalPage = total / pageSize + 1;
//            }

            ListPage listPage = new ListPage();
            listPage.total = total;
            listPage.rows = list;

            return listPage;
        } catch (Exception exception) {
            return null;
        }
    }

    public class ListPage {
        //        public int totalPage;//页码总数（多少页）
        public int total;//数据总数（一共多少数据）
        //        public int pageIndex;//当前在第几页
        public List<VersionInfo> rows;//当前页列表
    }

    /**
     * 添加
     *
     * @param param
     * @param request
     * @return
     */
    @RequestMapping(value = "/Add", method = RequestMethod.POST)
    public boolean Add(@RequestBody HashMap<String, String> param, HttpServletRequest request) {
        try {
            VersionInfo model = new VersionInfo();
            model.VersionName = param.get("VersionName");
            model.VersionNumber = param.get("VersionNumber");
            model.Stage = param.get("Stage");
            model.Submitter = param.get("Submitter");

            int result = versionInfoService.Add(model);
            if (result > 0)
                return true;
            else
                return false;
        } catch (Exception exception) {
            return false;
        }
    }

    @RequestMapping(value = "/Delete", method = RequestMethod.POST)
    public ResultDelete Delete(@RequestBody HashMap<String, Object> param, HttpServletRequest request) {
        ResultDelete resultDelete = new ResultDelete();
        resultDelete.result = false;
        resultDelete.des = "操作失败";

        try {
            int Id = (null != param.get("Id")) ? (Integer) param.get("Id") : null;

            if (Id == 0) {
            } else {
                boolean result = versionInfoService.Delete(Id);
                if (result) {
                    resultDelete.result = true;
                    resultDelete.des = "操作成功";
                } else {
                }
            }
        } catch (Exception e) {
            resultDelete.des = e.getMessage();
        }

        return resultDelete;
    }

    public class ResultDelete {
        public boolean result;
        public String des;
    }
}